package com.iterator.service.web.systemManager;

import com.github.pagehelper.PageHelper;
import com.iterator.bean.web.dhy.AuthInfoVo;
import com.iterator.bean.web.wy.admin.*;
import com.iterator.mapper.web.systemManager.AdminMapper;
import com.iterator.util.IteratorEncrypt;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: Silence_W
 * @date: 2021-01-10 15:00
 **/
@Service
public class AdminListServiceImpl implements AdminListService {

    @Autowired
    AdminMapper adminMapper;

    //管理员数据显示service
    @Override
    public AdminVO queryAdmin(Integer page, Integer limit, String username, String sort, String order) {
        PageHelper.startPage(page, limit);
        AdminExample adminExample = new AdminExample();
        adminExample.setOrderByClause(sort + " " + order);
        AdminExample.Criteria criteria = adminExample.createCriteria();
        if (username != null && username.length() != 0) {
            criteria.andUsernameLike("%" + username + "%");
        }

        List<Admin> admins = adminMapper.selectByExample(adminExample);
        ArrayList<AdminItem> adminItems = new ArrayList<>();
        for (Admin admin : admins) {

            List<Integer> parse = parse(admin);
            adminItems.add(new AdminItem(admin.getId(), admin.getUsername(), admin.getAvatar(), parse));
        }

        return new AdminVO(admins.size(), adminItems);


    }


    //插入管理员数据的
    @Transactional
    @Override
    public CreateVO insertAdmin(CreateBO createBO, HttpServletRequest request) {
        String encryptPassword = IteratorEncrypt.getEncryptPassword(createBO.getPassword());
        Admin admin = new Admin(null, createBO.getUsername(),encryptPassword , null, null, createBO.getAvatar(), new Date(), new Date(), false, createBO.getRoleIds().toString());
        //线程不安全
        int insert = adminMapper.insert(admin);
        Admin admin1 = adminMapper.selectByPrimaryKey(adminMapper.selectMaxId());
        ArrayList<Integer> integers = parse(admin1);

        //密码加密
        return new CreateVO(admin1.getId(), admin1.getAvatar(), admin1.getUsername(), encryptPassword, admin1.getAddTime().toString(), admin1.getUpdateTime().toString(), integers);
    }

    //将admin中的string roleId转化成List<integer>
    private ArrayList<Integer> parse(Admin admin1) {

        String roleIds = admin1.getRoleIds();
        String substring = roleIds.substring(1, roleIds.length() - 1);
        String[] split = substring.split(",");
        ArrayList<Integer> integers = new ArrayList<>();
        if (split[0].equals("")){
            return integers;

        }
        for (int i = 0; i < split.length; i++) {
            String trim = split[i].trim();
            integers.add(Integer.parseInt(trim));
        }
        return integers;
    }


    //删除管理员数据
    @Override
    public void delete(int id) {
        adminMapper.deleteByPrimaryKey(id);
    }


    //跟新管理员数据
    @Transactional
    @Override
    public UpdateVO updateAdmin(UpdateBO updateBO) {

        String encryptPassword = IteratorEncrypt.getEncryptPassword(updateBO.getPassword());

        Admin admin = new Admin(updateBO.getId(), updateBO.getUsername(), encryptPassword, null, null, updateBO.getAvatar(), null, new Date(), false, updateBO.getRoleIds().toString());
        //线程不安全
        adminMapper.updateByPrimaryKeySelective(admin);
        Admin admin1 = adminMapper.selectByPrimaryKey(updateBO.getId());
        List<Integer> parse = parse(admin1);
        return new UpdateVO(admin1.getId(), admin1.getUsername(),encryptPassword , admin1.getAvatar(), admin1.getUpdateTime().toString(), parse);
    }

    /**
     * 获取授权信息
     *
     * @return
     */
    @Override
    public AuthInfoVo getAuthInfo() {
        AuthInfoVo authInfoVo = new AuthInfoVo();
        //获取用户name
        String userName = (String) SecurityUtils.getSubject().getPrincipal();
        //封装userName
        authInfoVo.setName(userName);


        AdminExample adminExample = new AdminExample();
        if (userName != null && userName.length() != 0) {
            adminExample.createCriteria().andUsernameEqualTo(userName);
        }
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        Admin admin = admins.get(0);
        authInfoVo.setAvatar(admin.getAvatar());
        //获取权限角色信息
        List<Integer> roles = parse(admin);
        if (roles == null || roles.size() == 0) {
            List<String> roles2 = new ArrayList<>();
            authInfoVo.setRoles(roles2);
        } else {
            List<String> roles1 = adminMapper.selectRolesByroleId(roles);
            authInfoVo.setRoles(roles1);
        }
        //查询权限表permission
            List<String> perms = adminMapper.selectPermissionByroleId(roles);
            authInfoVo.setPerms(perms);

        return authInfoVo;
    }

    @Override
    public int authen(String username) {
        List<Admin> admins = adminMapper.selectAllByUsername(username);
        if (admins.size()>1){
            return 605;

        }else if(admins.size()==1){
            return 604;

        }        else {
            return 201;
        }
    }

    /**
     * 添加登录时间
     * @param username
     * @param host
     */
    @Override
    public void updateLoginTime(String username, String host) {
        Admin admin = new Admin();
        admin.setLastLoginIp(host);
        admin.setLastLoginTime(new Date());
        AdminExample adminExample = new AdminExample();
        adminExample.createCriteria().andUsernameEqualTo(username);
        adminMapper.updateByExampleSelective(admin,adminExample);

    }
}
